<?php

namespace Biz\service\activity\project;

use Biz\model\activity\BizProjectContractModel;
use Biz\model\activity\BizProjectGroupModel;
use Biz\model\activity\BizProjectModel;
use Doraemon\model\Admin\BeerUsUser;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;

class BizProjectContract extends BizProjectPageTpl
{

    const TPL_CONFIG_TPL_NAME = '合同收款信息';
    const CONTRACT_AMOUNT_NAME = ['三','四','五','六','七','八','九','十','十一','十二','十三'];

    public static function getAccountName($i = 0): string
    {
        return sprintf("第%d笔款", $i + 3);
    }

    public function condition(): array
    {
        return ['project_id' => $this->getPid()];
    }

    /**
     * @throws Exception
     */
    public function getList(): array
    {
        $this->authorError();
        $condition = ['ORDER' => ['sort' => 'ASC', 'id' => 'ASC'], 'LIMIT' => [$this->getOffset(), $this->getPageSize()]];
        $list = BizProjectContractModel::dataSelect($condition + $this->getRequestCondition() + BizProjectContractModel::statusCondition());
        $response = [];
        if ($list) {
            foreach ($list as $i => $v) {
                $response[] = $v + [
                    'name' => self::getAccountName($i),
                    'created_at_date' => $v['created_at'],
                ];
            }
        }
        return $response;
    }

    public function getTotal(): int
    {
        return BizProjectContractModel::statusTotal($this->getRequestCondition());
    }

    public function withInfoHtml(): array
    {
        $info = BizProjectModel::getAdmInfo($this->getPid());
        $projectInfo = $info['project'] ?? [];
        $html = [
            "项目编号 : " . ($projectInfo['no'] ?? ''),
            "项目名称 : " . ($projectInfo['name'] ?? ''),
            "项目金额 : " . number_format($projectInfo['money']  ?? 0, 2),
        ];

        return ["项目信息", implode("<br/>", $html)];
    }

    public function getGrid(Grid $grid)
    {
        $grid->disableFilter();
        $grid->column('id', 'ID')->sort();
        $grid->column('name', '收款信息');
        $grid->column('anticipated_amount', '预计收款金额');
        $grid->column('actual_amount', '实际收款金额');
        $grid->column('anticipated_time', '预计收款时间');
        $grid->column('actual_time', '实际收款时间');
        $grid->column('bill_time', '开票时间');
        $grid->column('bill_amount', '开票金额');
        $grid->column('bill_no', '发票号码');
        $grid->column('bill_express_no', '发票快递单号');
        $grid->column('created_at_date', '创建时间');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
    }

    public function sort($input)
    {
        BizProjectContractModel::action(function () use ($input) {
            foreach ($input as $id => $sort) {
                BizProjectContractModel::update(['sort' => $sort], ['id' => $id]);
            }
        });
    }


    public function getForm(Form $form)
    {
        $form->text('base.anticipated_amount', '预计收款金额')->help("仅数字，2位小数");
        $form->text('base.actual_amount', '实际收款金额')->help("仅数字，2位小数");
        $form->date('base.anticipated_time', '预计收款时间');
        $form->date('base.actual_time', '实际收款时间');
        $form->date('base.bill_time', '开票时间');
        $form->text('base.bill_amount', '开票金额')->help("仅数字，2位小数");
        $form->text('base.bill_no', '发票号码');
        $form->text('base.bill_express_no', '发票快递单号');
    }

    public function getFormInfo($id): array
    {
        $info = $id ? BizProjectContractModel::dataSelectInfo(['id' => $id] + BizProjectGroupModel::statusCondition()) : [];
        return ['base' => $info, 'id' => $id];
    }

    /**
     * @throws Exception
     */
    public function save($input)
    {
        $this->authorError();
        $base = $input['base'] ?? '';
        $id = $input['id'] ?? 0;
        BizProjectContractModel::addData($base + ['id' => $id, 'project_id' => $this->getPid()]);


    }

    /**
     * @throws Exception
     */
    public function delete($id)
    {
        $this->authorError();
        return BizProjectContractModel::statusDelete($id);
    }

    public function breadcrumb(): array
    {
        return [
            [
                'text' => BizProjectProject::getListTitle(), 'url' => BizProjectProject::getRoute()->getEditRoute($this->getPid())
            ],
        ];
    }

    /**
     * @throws Exception
     */
    public function authorError()
    {
        $auth = BizProjectGroup::fieldAuth($this->adminId(), 'contract');
        $auth = $auth['other_data'] ?? 0;
        if (!$this->saveForm($auth)) throw new Exception("权限不足");
    }
}

